package cn.lsnu.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sound.midi.Soundbank;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class RandomValidateCode {

    private Logger logger = LoggerFactory.getLogger(RandomValidateCode.class);

    private Random random = new Random();

    private String  randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private Integer width;

    private Integer height;

    private Integer lineSize;

    private Integer stringNum;

    private Integer fontSize;

    private String verifyCode;

    public RandomValidateCode() {
        this(null,null,null,null,null);
    }
    /**
     * @Description 绘制验证码
     * @param width: 图片宽度
     * @param height: 图片高度
     * @param lineSize: 干扰线数量
     * @param stringNum: 随机产生的字符数量
     Argument for @NotNull parameter 'type' of com/intellij/codeInsight/template/PsiTypeResult.<init> must not be null 
     * @author  lin 
     * @Datetime  2020/2/4 10:52
     */  
    public RandomValidateCode(Integer width, Integer height, Integer lineSize, Integer stringNum,Integer fontSize) {
        setWidth(width);
        setHeight(height);
        setLineSize(lineSize);
        setStringNum(stringNum);
        setFontSize(fontSize);
    }
    public  BufferedImage getRandCode(){

        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);

        Graphics g = image.getGraphics();
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setBackground(Color.WHITE);
//        g2d.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,32));
        g2d.clearRect(0, 0, width, height);
        g2d.setColor(getRandColor(160,200));
        g2d.fillRect(0,0,width,height);
        g2d.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,32));
        //绘制随机字符
        String randomString = " ";
        for(int i=1;i<=stringNum;i++){
            randomString = drowString(g2d,randomString,i);
        }
        //绘制干扰线
        for(int i=0;i<lineSize;i++){
            drowLine(g2d);
        }
        g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
        g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
        this.verifyCode = randomString;
        logger.info("验证码:"+randomString);
        g2d.dispose();
        return image;
    }


    /**
     * @Description  获取字体
     * @return java.awt.Font
     * @author  lin
     * @Datetime  2020/2/4 10:13
     */
    private Font getFont(){
        return  new Font("Fixedsys",Font.CENTER_BASELINE,fontSize);
    }

    /**
     * @Description 获取颜色
     * @param fc: 
     * @param bc: 
     * @return java.awt.Color 
     * @author  lin 
     * @Datetime  2020/2/4 10:17
     */  
    private Color getRandColor(int fc,int bc){
        fc = fc > 255?255:fc;
        bc = bc > 255?255:bc;
        int r = fc + random.nextInt(bc - fc - 16);
        int g = fc + random.nextInt(bc - fc - 14);
        int b = fc + random.nextInt(bc - fc - 18);
        return new Color(r,g,b);
    }

    /**
     * @Description   绘制字符串
     * @param g: 
     * @param randomString: 
     * @param i: 
     * @return java.lang.String 
     * @author  lin 
     * @Datetime  2020/2/4 10:29
     */  
    private String drowString(Graphics2D g,String randomString,int i){
        g.setFont(getFont());
        g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
        String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
        randomString=rand+randomString;
        FontMetrics fm = g.getFontMetrics();
        g.translate(random.nextInt(3),random.nextInt(3));
        int x = ((getWidth() - fm.stringWidth(randomString)) / 2);
        int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
        x = x == 1?x/4:x*2+i;
        g.drawString(rand, x, y+i);
        return  randomString;
    }

    /**
     * @Description  绘制干扰线
     * @param g: 
     * @return void 
     * @author  lin 
     * @Datetime  2020/2/4 10:23
     */  
    private void drowLine(Graphics2D g){
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int x1 = random.nextInt(13);
        int y1 = random.nextInt(15);
//        g.setColor(getColor(50));
        g.drawLine(x,y,x1,y1);
    }

    /**
     * @Description 获取随机字符串
     * @param num: 
     * @return java.lang.String 
     * @author  lin 
     * @Datetime  2020/2/4 10:25
     */  
    public String getRandomString(int num){
        return String.valueOf(randString.charAt(num));
    }


    private Color getColor(int alpha)
    {
        int R=(int) (Math.random()*255);
        int G=(int) (Math.random()*255);
        int B=(int) (Math.random()*255);
        return new Color(R,G,B,alpha);
    }
    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width == null ? Global.VALIDATECODE_WIDTH :width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height== null?Global.VALIDATECODE_HEIGHT:height;
    }

    public Integer getLineSize() {
        return lineSize;
    }

    public void setLineSize(Integer lineSize) {
        this.lineSize = lineSize == null?Global.VALIDATECODE_LINESIZE:lineSize;
    }

    public Integer getStringNum() {
        return stringNum;
    }

    public void setStringNum(Integer stringNum) {
        this.stringNum = stringNum == null?Global.VALIDATECODE_STRINGNUM:stringNum;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public Integer getFontSize() {
        return fontSize;
    }

    public void setFontSize(Integer fontSize) {
        this.fontSize = fontSize == null?Global.VALIDATECODE_FONTSIZE:fontSize;
    }

    public static void main(String args[]){
        RandomValidateCode randomValidateCode = new RandomValidateCode();
//        randomValidateCode.getRandCode();
//        System.out.println(randomValidateCode.getVerifyCode());

    }
}
